﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace br.ita.CTC34.GLCToAPConverter
{
    public class Automata
    {
        #region Properties Declarations
        public string initialState { get; set; }
        public string emptyStringSymbol { get; set; }
        public string emptyStackSymbol { get; set; }
        
        //guarda os estados que são diferentes de q0 e qf
        public ArrayList intermediateStates { get; set; }
        public ArrayList finalStates { get; set; }
        public ArrayList tapeSymbols { get; set; }
        public ArrayList stackSymbols { get; set; }
        public ArrayList transitions { get; set; }

        public static string EMPTY_STRING_SYMBOL = "Símbolo Da Cadeia Vazia";
        public static string EMPTY_STACK_SYMBOL = "Símbolo Da Pilha Vazia";
        public static string INITIAL_STATE = "Estado Inicial";
        public static string FINAL_STATES = "Estados Finais";
        public static string TAPE_SYMBOLS = "Símbolos da Fita";
        public static string TRANSITIONS = "Transições";
        public static string STACK_SYMBOLS = "Símbolos da Pilha";
        public static string COLON = ":";
        #endregion

        public override string ToString()
        {
            string ap = "";

            ap += INITIAL_STATE + COLON;
            ap += System.Environment.NewLine;

            ap += this.initialState;
            ap += System.Environment.NewLine;

            ap += System.Environment.NewLine;
            ap += EMPTY_STRING_SYMBOL + COLON;
            ap += System.Environment.NewLine;

            ap += this.emptyStringSymbol;
            ap += System.Environment.NewLine;

            ap += System.Environment.NewLine;
            ap += EMPTY_STACK_SYMBOL + COLON;
            ap += System.Environment.NewLine;

            ap += this.emptyStackSymbol;
            ap += System.Environment.NewLine;

            ap += System.Environment.NewLine;
            ap += FINAL_STATES + COLON;
            ap += System.Environment.NewLine;

            foreach (string s in this.finalStates)
            {
                ap += s + " ";
            }
            ap += System.Environment.NewLine;

            ap += System.Environment.NewLine;
            ap += TAPE_SYMBOLS + COLON;
            ap += System.Environment.NewLine;

            foreach (string c in this.tapeSymbols)
            {
                ap += c + " ";
            }
            ap += System.Environment.NewLine;

            ap += System.Environment.NewLine;
            ap += STACK_SYMBOLS + COLON;
            ap += System.Environment.NewLine;

            foreach (string c in this.stackSymbols)
            {
                ap += c + " ";
            }
            ap += System.Environment.NewLine;

            ap += System.Environment.NewLine;
            ap += TRANSITIONS + COLON;
            ap += System.Environment.NewLine;

            foreach (APTransition t in this.transitions)
            {
                ap += "("+ t.currentState + ", " + t.tapeSymbol + ", " + t.symbolReadFromStack + ")";
                ap += " -> (" + t.newState + ", " + t.symbolWrittenOnStack + ")";
                ap += System.Environment.NewLine;
            }
 
            return ap;
        }

        public string getAutomataOutputFileContent()
        {
            string fileContent = "";

            fileContent += this.initialState;
            fileContent += System.Environment.NewLine;

            fileContent += this.emptyStringSymbol + " " + this.emptyStackSymbol;
            fileContent += System.Environment.NewLine;

            foreach (string s in this.finalStates)
            {
                fileContent += s + " ";
            }

            fileContent.Trim();
            fileContent += System.Environment.NewLine;

            foreach (string c in this.tapeSymbols)
            {
                fileContent += c + " ";
            }

            fileContent.Trim();
            fileContent += System.Environment.NewLine;

            foreach (string c in this.stackSymbols)
            {
                fileContent += c + " ";
            }

            fileContent.Trim();
            fileContent += System.Environment.NewLine;

            foreach (APTransition t in this.transitions)
            {
                fileContent += t.currentState + " " + t.tapeSymbol + " " + t.symbolReadFromStack;
                fileContent += " > " + t.newState + " " + t.symbolWrittenOnStack;
                fileContent += System.Environment.NewLine;
            }

            return fileContent;
        }
    }
}
